import { h, Ref, unref } from 'vue';
import { Switch } from 'ant-design-vue';
import { BasicColumn, FormSchema } from '/@/components/Table';
import { useI18n } from '/@/hooks/web/useI18n';
import { useMessage } from '/@/hooks/web/useMessage';
import { getAllFeatureContributeYears } from '/@/api/basic/qimo/index';
import { TenantStatusEnum } from '/@/enums/biz/tenant';
import {
  dictComponentProps,
  stateComponentProps,
  stateFilters,
  yesNoComponentProps,
} from '/@/utils/lamp/common';
import { ActionEnum, DictEnum, FileBizTypeEnum } from '/@/enums/commonEnum';
import { FormSchemaExt, RuleType } from '/@/api/lamp/common/formValidateService';
import {
  checkEmail,
  checkIdCard,
  checkMobile,
  checkUsername,
  updateState,
} from '/@/api/devOperation/tenant/defUser';

const { t } = useI18n();
const { createMessage } = useMessage();
const citiesOptionsData = [
  { key: '520154675945986049', label: '计算机与信息技术学院', value: '1' },
  { key: '520154675945986052', label: '商学院', value: '2' },
  { key: '520154675945986055', label: '教务处', value: '3' },
  { key: '526891104516526724', label: '智能制造技术学院', value: '4' },
  { key: '526891104516526727', label: '仲景中医药与健康学院', value: '5' },
  { key: '526891104516526732', label: '教育学院', value: '6' },
  { key: '526891104516526749', label: '建筑与艺术设计学院', value: '7' },
  { key: '526891104516526752', label: '体育学院', value: '8' },
  { key: '526891104516526755', label: '中专部', value: '9' },
  { key: '526891104516526758', label: '公共教学部', value: '10' },
  { key: '526891104516526761', label: '马克思主义学院', value: '11' },
  { key: '526891104516526764', label: '继续教育学院', value: '12' },
  { key: '526891104516526767', label: '党群工作部', value: '13' },
  { key: '526891104516526770', label: '党政办公室', value: '14' },
  { key: '526891104516526774', label: '发展规划处（创新创业学院）', value: '15' },
  { key: '526891104516526777', label: '人力资源部', value: '16' },
  { key: '526891104516526780', label: '科技外事处', value: '17' },
  { key: '526891104516526783', label: '学生工作处', value: '18' },
  { key: '526891104516526786', label: '法务监督部', value: '19' },
  { key: '526891104516526789', label: '招生工作办公室', value: '20' },
  {
    key: '526891104516526796',
    label: '大学生实习实训与就业指导中心（校地合作处）',
    value: '21',
  },
  { key: '526891104516526799', label: '财务处', value: '22' },
  { key: '526891104516526802', label: '基建处', value: '23' },
  { key: '526891104516526805', label: '保卫处', value: '24' },
  { key: '526891104516526808', label: '后勤管理处', value: '25' },
  { key: '526891104516526813', label: '心理健康教育中心', value: '26' },
  { key: '526891104516526823', label: '图书馆', value: '27' },
  { key: '526891104516526831', label: '后勤服务集团直属党支部', value: '28' },
];

// const years = await getAllFeatureContributeYears();
// const yaerList = [];
// for (let i = 0; i < years.length; i++) {
//   const as = { value: Number(years[i]), label: years[i], key: i + 1 };
//   const vs = {
//     value: as,
//     label: as,
//   };
//   yaerList.push(vs);
// }
// as = [
//   { value: 2024, label: 2024 },
//   { value: 2023, label: 2023 },
// ];
// 列表页字段
export const columns = (): BasicColumn[] => {
  return [
    {
      title: '提交部门',
      dataIndex: 'tenantId',
      customRender: ({ record }) => {
        String(record.tenantId);
        if (record.tenantId == '520154675945986049') {
          return <span style={{ textAlign: 'center' }}>计算机与信息技术学院</span>;
        } else if (record.tenantId == '520154675945986052') {
          return <span style={{ textAlign: 'center' }}>商学院</span>;
        } else if (record.tenantId == '520154675945986055') {
          return <span style={{ textAlign: 'center' }}>教务处</span>;
        } else if (record.tenantId == '526891104516526724') {
          return <span style={{ textAlign: 'center' }}>智能制造技术学院</span>;
        } else if (record.tenantId == '526891104516526727') {
          return <span style={{ textAlign: 'center' }}>仲景中医药与健康学院</span>;
        } else if (record.tenantId == '526891104516526732') {
          return <span style={{ textAlign: 'center' }}>教育学院</span>;
        } else if (record.tenantId == '526891104516526749') {
          return <span style={{ textAlign: 'center' }}>建筑与艺术设计学院</span>;
        } else if (record.tenantId == '526891104516526752') {
          return <span style={{ textAlign: 'center' }}>体育学院</span>;
        } else if (record.tenantId == '526891104516526755') {
          return <span style={{ textAlign: 'center' }}>中专部</span>;
        } else if (record.tenantId == '526891104516526758') {
          return <span style={{ textAlign: 'center' }}>公共教学部</span>;
        } else if (record.tenantId == '526891104516526761') {
          return <span style={{ textAlign: 'center' }}>马克思主义学院</span>;
        } else if (record.tenantId == '526891104516526764') {
          return <span style={{ textAlign: 'center' }}>继续教育学院</span>;
        } else if (record.tenantId == '526891104516526767') {
          return <span style={{ textAlign: 'center' }}>党群工作部</span>;
        } else if (record.tenantId == '526891104516526770') {
          return <span style={{ textAlign: 'center' }}>党政办公室</span>;
        } else if (record.tenantId == '526891104516526774') {
          return <span style={{ textAlign: 'center' }}>发展规划处（创新创业学院）</span>;
        } else if (record.tenantId == '526891104516526777') {
          return <span style={{ textAlign: 'center' }}>人力资源部</span>;
        } else if (record.tenantId == '526891104516526780') {
          return <span style={{ textAlign: 'center' }}>科技外事处</span>;
        } else if (record.tenantId == '526891104516526783') {
          return <span style={{ textAlign: 'center' }}>学生工作处</span>;
        } else if (record.tenantId == '526891104516526786') {
          return <span style={{ textAlign: 'center' }}>法务监督部</span>;
        } else if (record.tenantId == '526891104516526789') {
          return <span style={{ textAlign: 'center' }}>招生工作办公室</span>;
        } else if (record.tenantId == '526891104516526796') {
          return (
            <span style={{ textAlign: 'center' }}>大学生实习实训与就业指导中心（校地合作处）</span>
          );
        } else if (record.tenantId == '526891104516526799') {
          return <span style={{ textAlign: 'center' }}>财务处</span>;
        } else if (record.tenantId == '526891104516526802') {
          return <span style={{ textAlign: 'center' }}>基建处</span>;
        } else if (record.tenantId == '526891104516526805') {
          return <span style={{ textAlign: 'center' }}>保卫处</span>;
        } else if (record.tenantId == '526891104516526808') {
          return <span style={{ textAlign: 'center' }}>后勤管理处</span>;
        } else if (record.tenantId == '526891104516526813') {
          return <span style={{ textAlign: 'center' }}>心理健康教育中心</span>;
        } else if (record.tenantId == '526891104516526823') {
          return <span style={{ textAlign: 'center' }}>图书馆</span>;
        } else if (record.tenantId == '526891104516526831') {
          return <span style={{ textAlign: 'center' }}>后勤服务集团直属党支部</span>;
        }
      },
      // width: 180,
    },
    {
      title: '特色（贡献）项目名称',
      dataIndex: 'projectName',
      // width: 180,
    },
    {
      title: '是否独立完成',
      dataIndex: 'isSelfFinish',
      customRender: ({ record }) => {
        if (record.isSelfFinish) {
          return <span style={{ textAlign: 'center' }}>是</span>;
        } else {
          return <span style={{ textAlign: 'center' }}>否</span>;
        }
      },
      width: 80,
    },

    {
      title: '协作部门',
      dataIndex: 'collTenantIds',
      customRender: ({ record }) => {
        let str = '';
        if (record.collTenantIds) {
          for (let k = 0; k < record.collTenantIds.length; k++) {
            if (record.collTenantIds[k] == '520154675945986049') {
              k == 0
                ? (str = `${str} 计算机与信息技术学院`)
                : (str = `${str}, 计算机与信息技术学院`);
            } else if (record.collTenantIds[k] == '520154675945986052') {
              k == 0 ? (str = `${str} 商学院`) : (str = `${str}, 商学院`);
            } else if (record.collTenantIds[k] == '520154675945986055') {
              k == 0 ? (str = `${str} 教务处`) : (str = `${str}, 教务处`);
            } else if (record.collTenantIds[k] == '526891104516526724') {
              k == 0 ? (str = `${str} 智能制造技术学院`) : (str = `${str}, 智能制造技术学院`);
            } else if (record.collTenantIds[k] == '526891104516526727') {
              k == 0
                ? (str = `${str} 仲景中医药与健康学院`)
                : (str = `${str}, 仲景中医药与健康学院`);
            } else if (record.collTenantIds[k] == '526891104516526732') {
              k == 0 ? (str = `${str} 教育学院`) : (str = `${str}, 教育学院`);
            } else if (record.collTenantIds[k] == '526891104516526749') {
              k == 0 ? (str = `${str} 建筑与艺术设计学院`) : (str = `${str}, 建筑与艺术设计学院`);
            } else if (record.collTenantIds[k] == '526891104516526752') {
              k == 0 ? (str = `${str} 体育学院`) : (str = `${str}, 体育学院`);
            } else if (record.collTenantIds[k] == '526891104516526755') {
              k == 0 ? (str = `${str} 中专部`) : (str = `${str}, 中专部`);
            } else if (record.collTenantIds[k] == '526891104516526758') {
              k == 0 ? (str = `${str} 公共教学部`) : (str = `${str}, 公共教学部`);
            } else if (record.collTenantIds[k] == '526891104516526761') {
              k == 0 ? (str = `${str} 马克思主义学院`) : (str = `${str}, 马克思主义学院`);
            } else if (record.collTenantIds[k] == '526891104516526764') {
              k == 0 ? (str = `${str} 继续教育学院`) : (str = `${str}, 继续教育学院`);
            } else if (record.collTenantIds[k] == '526891104516526767') {
              k == 0 ? (str = `${str} 党群工作部`) : (str = `${str}, 党群工作部`);
            } else if (record.collTenantIds[k] == '526891104516526770') {
              k == 0 ? (str = `${str} 党政办公室`) : (str = `${str}, 党政办公室`);
            } else if (record.collTenantIds[k] == '526891104516526774') {
              k == 0
                ? (str = `${str} 发展规划处（创新创业学院）`)
                : (str = `${str}, 发展规划处（创新创业学院）`);
            } else if (record.collTenantIds[k] == '526891104516526777') {
              k == 0 ? (str = `${str} 人力资源部`) : (str = `${str}, 人力资源部`);
            } else if (record.collTenantIds[k] == '526891104516526780') {
              k == 0 ? (str = `${str} 科技外事处`) : (str = `${str}, 科技外事处`);
            } else if (record.collTenantIds[k] == '526891104516526783') {
              k == 0 ? (str = `${str} 学生工作处`) : (str = `${str}, 学生工作处`);
            } else if (record.collTenantIds[k] == '526891104516526786') {
              k == 0 ? (str = `${str} 法务监督部`) : (str = `${str}, 法务监督部`);
            } else if (record.collTenantIds[k] == '526891104516526789') {
              k == 0 ? (str = `${str} 招生工作办公室`) : (str = `${str}, 招生工作办公室`);
            } else if (record.collTenantIds[k] == '526891104516526796') {
              k == 0
                ? (str = `${str} 大学生实习实训与就业指导中心（校地合作处）`)
                : (str = `${str}, 大学生实习实训与就业指导中心（校地合作处）`);
            } else if (record.collTenantIds[k] == '526891104516526799') {
              k == 0 ? (str = `${str} 财务处`) : (str = `${str}, 财务处`);
            } else if (record.collTenantIds[k] == '526891104516526802') {
              k == 0 ? (str = `${str} 基建处`) : (str = `${str}, 基建处`);
            } else if (record.collTenantIds[k] == '526891104516526805') {
              k == 0 ? (str = `${str} 保卫处`) : (str = `${str}, 保卫处`);
            } else if (record.collTenantIds[k] == '526891104516526808') {
              k == 0 ? (str = `${str} 后勤管理处`) : (str = `${str}, 后勤管理处`);
            } else if (record.collTenantIds[k] == '526891104516526813') {
              k == 0 ? (str = `${str} 心理健康教育中心`) : (str = `${str}, 心理健康教育中心`);
            } else if (record.collTenantIds[k] == '526891104516526823') {
              k == 0 ? (str = `${str} 图书馆`) : (str = `${str}, 图书馆`);
            } else if (record.collTenantIds[k] == '526891104516526831') {
              k == 0
                ? (str = `${str} 后勤服务集团直属党支部`)
                : (str = `${str}, 后勤服务集团直属党支部`);
            }
          }
        }
        return <span style={{ textAlign: 'center' }}>{str}</span>;
      },
    },
    {
      title: '项目类别',
      dataIndex: 'projectType',
      // sorter: true,
      width: 90,
      customRender: ({ record }) => {
        if (record.projectType == 'A') {
          return <span style={{ textAlign: 'center' }}>特色项目</span>;
        } else if (record.projectType == 'B') {
          return <span style={{ textAlign: 'center' }}>贡献项目</span>;
        }
      },
    },
    {
      title: '特色创新项目概述',
      dataIndex: 'projectDesc',
      customRender: ({ record }) => {
        return <span style={{ textAlign: 'center' }}>{record.projectDesc}</span>;
      },
      // sorter: true,
    },
  ];
};

export const searchFormSchema = (): FormSchema[] => {
  return [
    {
      label: '年份',
      field: 'year',
      component: 'ApiSelect',
      colProps: { span: 12 },
      componentProps: {
        api: getAllFeatureContributeYears,
        labelField: 'lable',
        valueField: 'year',
        showSearch: true,
        // mode: 'multiple', 变成数组形式
      },
      // defaultValue: new Date().getFullYear(),
      // componentProps: {
      //   options: Array.from({ length: 20 }, (_, index) => ({
      //     label: new Date().getFullYear() + index + '',
      //     value: new Date().getFullYear() + index,
      //   })),
      //   defaultValue: new Date().getFullYear(),
      // },
    },
  ];
};
// 下载年份选择
export const searchFormSchemas = (): FormSchema[] => {
  return [
    {
      label: '年份',
      field: 'year',
      component: 'ApiSelect',
      colProps: { span: 12 },
      componentProps: {
        api: getAllFeatureContributeYears,
        labelField: 'lable',
        valueField: 'year',
        showSearch: true,
      },
      // componentProps: {
      //   options: Array.from({ length: 20 }, (_, index) => ({
      //     label: new Date().getFullYear() + index + '',
      //     value: new Date().getFullYear() + index,
      //   })),
      //   defaultValue: new Date().getFullYear(),
      // },
    },
  ];
};
// 编辑页字段
export const editFormSchema = (type: Ref<ActionEnum>): FormSchema[] => {
  return [
    {
      field: 'id',
      component: 'Input',
      label: 'ID',
      show: false,
    },
    {
      field: 'tenantId',
      component: 'Input',
      label: 'IDS',
      show: false,
    },
    {
      field: 'divider-selects1',
      component: 'Divider',
      label: '特色（贡献）奖申报',
      required: true,
    },
    {
      field: 'projectName',
      label: '特色（贡献）项目名称',
      component: 'Input',
      dynamicDisabled: () => {
        return [ActionEnum.VIEW, ActionEnum.PingFEn].includes(type.value);
      },
      // required: false,
      required: true,
      // dynamicDisabled: false,
      // show: false,
    },
    {
      field: 'projectType',
      label: '项目类别',
      component: 'Select',
      required: true,
      componentProps: {
        options: [
          { value: 'A', label: '特色项目', key: '1' },
          { value: 'B', label: '贡献项目', key: '2' },
        ],
      },
      dynamicDisabled: () => {
        return [ActionEnum.VIEW, ActionEnum.PingFEn].includes(type.value);
      },
      helpMessage: [
        '一、特色奖申报条件：',
        '(一)申报项目经过不断培育，在工作制度、方法、途径等方面实现了创新，具有原始创新性、创造性',
        '(二)经实践检验，有利于推动学校工作开展和事业发展',
        '(三)对学校其他单位相关工作（或其他高校）具有引领和示范作用，便于推广复制',
        '(四)申报项目必须符合以上3个条件，方可申报特色奖',
        '二、贡献奖申报条件',
        '(一)在考核年度内取得了重大成绩，实现了学校某一方面工作的历史性突破，有利于扩大学校的办学声誉和影响力。可参考但不局限于以下条件:',
        '1.在省级及以上竞赛、表演、展览等重大活动取得优异成绩，获得省级及以上荣誉（奖励、表彰）',
        '2.先进事迹或某项成绩突出、实绩显著的工作被省级及以上媒体报道，在社会上产生广泛影响',
        '3.典型经验被主管部门及以上政府经验推广',
        '4.获得政府及政府部门较大额度的资金支持',
        '5.在学校举办的重大活动或重要工作中表现突出、产生重大影响的',
        '(二)在目标任务中明确承担的，无法纳入统一考核指标体系进行横向比较，且对学校事业发展具有全局性影响的重大任务，在任务完成时可比照申请贡献奖励（已进行其他单项奖励的除外）',
        '(三)申报项目符合以上任何1个条件，均可申报贡献奖。',
      ],
    },
    {
      field: 'isSelfFinish',
      label: '是否独立完成',
      component: 'Select',
      required: true,
      componentProps: {
        options: [
          { value: true, label: '是', key: '1' },
          { value: false, label: '否', key: '2' },
        ],
      },
      dynamicDisabled: () => {
        return [ActionEnum.VIEW, ActionEnum.PingFEn].includes(type.value);
      },
      helpMessage: ['独立完成选择‘是’--不能选择协作部门', '独立完成选择‘否’--可选择协作部门'],
    },
    {
      label: '协作部门',
      field: 'collTenantIds',
      component: 'ApiTransfer',
      required: false,
      helpMessage: [
        '操作注意：',
        '1.协作部门左边部门选择后务必点击中间的移动指示‘>’按钮将协作部门移动到右侧，方为选中协作部门',
        '2.协作部门右边部门选择后务必点击中间的移动指示‘<’按钮将协作部门移动到左侧，方为移除协作部门',
        '3.协作部门若只选择不点击移动按钮：‘>’‘<’，则无法提交；',
      ],
      componentProps: {
        render: (item) => item.label,
        api: async () => {
          return Promise.resolve(citiesOptionsData);
        },
      },
      dynamicDisabled: () => {
        return [ActionEnum.VIEW, ActionEnum.PingFEn].includes(type.value);
      },
    },
    {
      field: 'isLeaderReview',
      label: '分管校领导/联系校领导是否审核',
      required: true,
      // label: '领导是否审核把关',
      component: 'Select',
      componentProps: {
        options: [
          { value: true, label: '是', key: '1' },
          { value: false, label: '否', key: '2' },
        ],
      },
      dynamicDisabled: () => {
        return [ActionEnum.VIEW, ActionEnum.PingFEn].includes(type.value);
      },
    },
    {
      field: 'projectDesc',
      label: '特色创新项目概述',
      required: true,
      component: 'InputTextArea',
      dynamicDisabled: () => {
        return [ActionEnum.VIEW, ActionEnum.PingFEn].includes(type.value);
      },
      // ifShow: () => {
      //   return type.value !=== ActionEnum.VIEW || type.value === ActionEnum.PingFEn;
      // },
    },
    {
      field: 'initialEvalOpinion',
      label: '初评意见',
      component: 'InputTextArea',
      dynamicDisabled: () => {
        return [ActionEnum.VIEW].includes(type.value);
      },
      ifShow: () => {
        return type.value === ActionEnum.VIEW || type.value === ActionEnum.PingFEn;
      },
    },
    {
      field: 'leadGroupOpinion',
      label: '领导小组意见',
      component: 'InputTextArea',
      dynamicDisabled: () => {
        return [ActionEnum.VIEW].includes(type.value);
      },
      ifShow: () => {
        return type.value === ActionEnum.VIEW || type.value === ActionEnum.PingFEn;
      },
    },
    {
      field: 'divider-selects1',
      component: 'Divider',
      label: '佐证材料',
    },
    // {
    //   field: 'divider-selects1',
    //   component: 'Divider',
    //   label: '佐证材料',
    // }
    // {
    //   field: 'logos',
    //   label: '佐证材料',
    //   component: 'Upload',
    //   componentProps: {
    //     uploadParams: {
    //       bizType: FileBizTypeEnum.DEF_TENANT_LOGO,
    //     },
    //     multiple: false,
    //     maxNumber: 1,
    //     accept: ['pdf', '.xlsx', 'docx'],
    //   },
    //   colProps: {
    //     span: 12,
    //   },
    // },
  ];
};

// 重置密码
export const restPasswordFormSchema = (): FormSchema[] => {
  return [
    {
      field: 'id',
      label: 'ID',
      component: 'Input',
      show: false,
    },
    {
      label: '使用系统内置密码',
      field: 'isUseSystemPassword',
      component: 'RadioButtonGroup',
      componentProps: {
        ...yesNoComponentProps(),
      },
      defaultValue: true,
    },
    {
      label: t('devOperation.tenant.defUser.password'),
      field: 'password',
      component: 'InputPassword',
      componentProps: {
        autocomplete: 'off',
        placeholder: '当前密码',
      },
      ifShow: ({ values }) => {
        return !values.isUseSystemPassword;
      },
    },
    {
      label: '确认密码',
      field: 'confirmPassword',
      component: 'InputPassword',
      componentProps: {
        autocomplete: 'off',
        placeholder: '当前密码',
      },
      ifShow: ({ values }) => {
        return !values.isUseSystemPassword;
      },
    },
  ];
};

export const restPasswordFormSchemaRules = (
  getFieldsValue: () => Recordable,
): Partial<FormSchemaExt>[] => {
  return [
    {
      field: 'confirmPassword',
      type: RuleType.append,
      rules: [
        {
          trigger: ['change', 'blur'],
          required: true,
          validator: async (_, value) => {
            if (!value) {
              return Promise.reject('确认密码不能为空');
            }
            const model = await getFieldsValue();
            if (value !== model.password) {
              return Promise.reject('两次输入的密码不一致!');
            }
            return Promise.resolve();
          },
        },
      ],
    },
  ];
};
